import Loader from "./loader.js";
import Util from "./util.js";
import Formatter from "./formatter.js";
import Enums from "./enums.js";
import Account from "./account.js";
import Config from "../config.js";
import Store from "./store";
import Database from "./database";
import Viewer from "./viewer";
import Services from "./services";
import BaseJob from "./basejob";
import Jobs from "../schedules/install";
import ResourceLoader from "./resourceLoader.js";
import Theme from "./theme.js";
import Modules from "./modules";

class Framework {
  constructor($f7) {
    this.$f7 = $f7;
    this.db.init();
    this.store.init();
    this.jobs = {};

    Jobs.forEach((j) => {
      j.install(this);
    });
  }

  /**
   * @returns {Viewer}
   */
  get viewer() {
    return Services.getInstance(Viewer, this);
  }

  /**
   * @returns {Database}
   */
  get db() {
    return Services.getInstance(Database, Config.info.id);
  }

  /**
   * @returns {Account}
   */
  get account() {
    return Services.getInstance(Account, this);
  }

  /**
   * @returns {Store}
   */
  get store() {
    return Services.getInstance(Store, this);
  }

  /**
   * @returns {ResourceLoader}
   */
  get resourceLoader() {
    return Services.getInstance(ResourceLoader, this);
  }

  /**
   * @returns {Loader}
   */
  get Loader() {
    return Loader;
  }

  get modules() {
    return Services.getInstance(Modules, this);
  }

  // get db() {
  //     if (db == undefined) {
  //         let dbType = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB;
  //         db = new Dexie('championsoft', { indexedDB: dbType });
  //         db.Dexie = Dexie;
  //     }
  //     return db;
  // }

  /**
   * @returns {Formatter}
   */
  get formatter() {
    return Formatter;
  }

  /**
   * @returns {Util}
   */
  get util() {
    return Util;
  }

  get enums() {
    return Enums;
  }

  /**
   * @returns {Config}
   */
  get config() {
    return Config;
  }

  /**
   * @returns {Theme}
   */
  get theme() {
    return Services.getInstance(Theme, this);
  }

  get redirect() {
    return {
      home: () => {
        this.$f7.views.current.router.navigate(Config.pages.home);
      },
      login: () => {
        this.$f7.views.current.router.navigate(Config.pages.login);
      },
      register: () => {
        this.$f7.views.current.router.navigate(Config.pages.register);
      },
      to: (url) => {
        this.$f7.views.current.router.navigate(Config.pages[url] || url);
      },
      back: () => {
        this.$f7.views.current.router.back({force:true})
      },
    };
  }

  /**
   * @returns {BaseJob}
   */
  getJob(key) {
    return this.jobs[key];
  }
}

export default Framework;
